
#include "uart_dev.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"

static const int RX_BUF_SIZE = 4096*2;

#define TXD_PIN (1)
#define RXD_PIN (2)
#define UART_NUM 1

void uart_init(void) 
{
    const uart_config_t uart_config = {
        .baud_rate = 19200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    // We won't use a buffer for sending data.
    uart_driver_install(UART_NUM, RX_BUF_SIZE, 0, 0, NULL, 0);
    uart_param_config(UART_NUM, &uart_config);
    uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}

void uart_write_buffer(uint8_t *buffer,uint16_t length)
{
    uart_write_bytes(UART_NUM, buffer,length);
}

uint8_t uart_read_byte(void)
{
    uint8_t temp;
    uart_read_bytes(UART_NUM, &temp, 1, 500 / portTICK_PERIOD_MS);
    return temp;
}

void uart_start(void)
{
    uart_init();
}





